#include "SubTask.h"
#include "HttpModule.h"

FSubHttpTask::FSubHttpTask()
	:TaskID(INDEX_NONE),
	bFinished(false),
	StartPos(0),
	Size(0),
	bWaitResponse(false)
{
}

FSubHttpTask::~FSubHttpTask()
{
	Stop();
}

TSharedPtr<IHttpRequest> FSubHttpTask::CreateRequest()
{
	RequestPtr = FHttpModule::Get().CreateRequest();
	RequestPtr->SetURL(URL);
	RequestPtr->SetVerb(TEXT("GET"));
	RequestPtr->SetHeader(TEXT("Range"), Range);
	bWaitResponse = true;
	RequestTime = FApp::GetCurrentTime();
	return RequestPtr;
}

void FSubHttpTask::Stop()
{
	if (RequestPtr.IsValid())
	{
		RequestPtr->CancelRequest();
		RequestPtr = nullptr;
	}
}

void FSubHttpTask::SaveData()
{
	FString SubTaskFileName = MD5Str + TEXT("_") + FString::FromInt(TaskID) + TEXT(".hcf");

	FFileHelper::SaveArrayToFile(RawData, *(CurFilePath / SubTaskFileName));
}

void FSubHttpTask::LoadData()
{
	FString SubTaskFileName = MD5Str + TEXT("_") + FString::FromInt(TaskID) + TEXT(".hcf");
	FFileHelper::LoadFileToArray(RawData, *(CurFilePath / SubTaskFileName));
}
